
!-------------------------------------------------------------------------------------
subroutine read_input()
!-------------------------------------------------------------------------------------

  use dimensions
  use parameters

  implicit none

  logical :: filefound
  integer :: fin, fout

  fin  = 1
  fout = 2

  !-- print header to output file --

  open(fout, file=trim(runname)//'.out')
  write(fout,*)
  write(fout,*) 'Code Vlasov 2D'
  write(fout,*)
  write(fout,*) 'Reading input file'
  close(fout)


  !-- check if input file exist --

  inquire(file = trim(runname)//'.in', exist=filefound)
  if(.not.filefound) then
     open(fout, file = trim(runname)//'.out', position='append')
     write(fout,*) 'ERROR: cannot find the input file'
     close(fout)
  end if

  open(fin,file = trim(runname)//'.in',form='formatted')


  !-- background electric field --

  read(fin,*) 
  read(fin,*) 
  read(fin,*)

  read(fin,*,ERR=502,END=502)  phi0
  read(fin,*,ERR=502,END=502)  kz
  read(fin,*,ERR=502,END=502)  vPhase
  read(fin,*,ERR=502,END=502)  iE0Modulation


  !-- domain size --

  read(fin,*) 
  read(fin,*) 
  read(fin,*)

  read(fin,*,ERR=502,END=502)  nz
  read(fin,*,ERR=502,END=502)  Lx
  read(fin,*,ERR=502,END=502)  Vmax


  !-- initial conditions --

  read(fin,*) 
  read(fin,*) 
  read(fin,*)

  read(fin,*,ERR=502,END=502)  u
  read(fin,*,ERR=502,END=502)  SpeckleWidth
  read(fin,*,ERR=502,END=502)  iModx
  read(fin,*,ERR=502,END=502)  iModz
  read(fin,*,ERR=502,END=502)  ModAmp


  !-- numerical parameters --

  read(fin,*) 
  read(fin,*) 
  read(fin,*)

  read(fin,*,ERR=502,END=502)  restart
  read(fin,*,ERR=502,END=502)  nt
  read(fin,*,ERR=502,END=502)  dt
  read(fin,*,ERR=502,END=502)  iDiag
  read(fin,*,ERR=502,END=502)  iData
  read(fin,*,ERR=502,END=502)  Dxxxx
  read(fin,*,ERR=502,END=502)  Dvvvv
  read(fin,*,ERR=502,END=502)  KxFilter
  read(fin,*,ERR=502,END=502)  iself
  read(fin,*,ERR=502,END=502)  iWaveFrame
  read(fin,*,ERR=502,END=502)  iBcPerpPeriodic
  read(fin,*,ERR=502,END=502)  iFourier
  read(fin,*,ERR=502,END=502)  idfMod
  read(fin,*,ERR=502,END=502)  iFixPump

  close(fin)

  goto 503

502 continue
  open(fout, file = trim(runname)//'.out',  position='append')
  write(fout, *) 'ERROR: cannot process input file'
  close(fout)
  stop

503 continue
  open(fout, file = trim(runname)//'.out',  position='append')
  write(fout,*) 'Done reading input file.'


!-------------------------------------------------------------------------------------
!  check input
!-------------------------------------------------------------------------------------

 
  write(fout, *)
  write(fout, *)"Precompiled system size:"
  write(fout, *)
  write(fout, "('    jx =              ', (I6) )" ) jx
  write(fout, "('    jz =              ', (I6) )" ) jz
  write(fout, "('    jv =              ', (I6) )" ) jv
  write(fout, "('    jDirection =      ', (I6) )" ) jDirection

  write(fout, *)
  write(fout, *)"Input parameters:"
  write(fout, *)
  write(fout, "('    phi0 =            ', (e19.8) )" ) phi0
  write(fout, "('    kz =              ', (e19.8) )" ) kz
  write(fout, "('    vPhave =          ', (e19.8) )" ) vPhase
  write(fout, "('    iE0Modulation =   ', (I6) )" )    iE0Modulation

  write(fout, *)
  write(fout, "('    nz =              ', (I6) )" )    nz
  write(fout, "('    Lx =              ', (e19.8) )" ) Lx
  write(fout, "('    Vmax =            ', (e19.8) )" ) Vmax

  write(fout, *)
  write(fout, "('    u =               ', (e19.8) )" ) u
  write(fout, "('    SpeckleWidth =    ', (e19.8) )" ) SpeckleWidth
  write(fout, "('    iModx =           ', (I6) )" )    iModx
  write(fout, "('    iModz =           ', (I6) )" )    iModz
  write(fout, "('    ModAmp =          ', (e19.8) )" ) ModAmp

  write(fout, *)
  write(fout, "('    restart =         ', (I6) )" )    restart
  write(fout, "('    nt =              ', (I6) )" )    nt
  write(fout, "('    dt =              ', (e19.8) )" ) dt
  write(fout, "('    iDiag =           ', (I6) )" )    iDiag
  write(fout, "('    iData =           ', (I6) )" )    iData
  write(fout, "('    Dxxxx =           ', (e19.8) )" ) Dxxxx
  write(fout, "('    Dvvvv =           ', (e19.8) )" ) Dvvvv
  write(fout, "('    KxFilter =        ', (e19.8) )" ) KxFilter
  write(fout, "('    iself =           ', (I6) )" )    iself
  write(fout, "('    iWaveFrame =      ', (I6) )" )    iWaveFrame
  write(fout, "('    iBcPerpPeriodic = ', (I6) )" )    iBcPerpPeriodic
  write(fout, "('    iFourier =        ', (I6) )" )    iFourier
  write(fout, "('    idfMod =          ', (I6) )" )    idfMod
  write(fout, "('    iFixPump =        ', (I6) )" )    iFixPump

  write(fout, *)



  if(   (iFourier.ne.1).and.(iFourier.ne.-1).and.(iFourier.ne.0)  ) then
     write(fout,*) 'ERROR: input variable iFourier must be in (-1, 0, 1)'
     close(fout)
     stop
  endif


  if ( (iBcPerpPeriodic.EQ.1) .and. ( (jx/2)*2 .NE. jx) ) then
     write(fout,*) &
          'ERROR: jx not even but periodic boundary condition'
     close(fout)
     stop
  endif

  if( nz > jz/2 -1) then
     write(fout,*) 'ERROR: nz is too big'
     close(fout)
     stop
  end if

  ! iself == -1: prescribed total potential, Ez2D = E0z
  ! iself ==  0: non self-consistent potential with self-consistent harmonics
  ! iself ==  1: self consistent, external potential

  close(fout)

end subroutine read_input

!-------------------------------------------------------------------------------------
